namespace BM.Tools.WebControls.MForm.Navigation.NodeTests
{
    using System.Collections.Generic;
    using System.Linq;

    using BM.Tools.WebControls.MForm.Navigation.XPath.NodeTests;

    /// <summary>
    /// A navigator for the <see cref="ElementTest" /> node test locator.
    /// </summary>
    /// <typeparam name="T">
    /// The type of the navigable object.
    /// </typeparam>
    public class ElementTestNavigator<T> : NodeTestNavigator<T, ElementTest>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ElementTestNavigator{T}"/> class. 
        /// </summary>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="locator">
        /// The locator.
        /// </param>
        public ElementTestNavigator(INavigator<T> parent, ElementTest locator) : base(parent, locator)
        {
        }

        /// <summary>
        /// Returns all children elements.
        /// </summary>
        /// <returns>
        /// the enumerable of matching items
        /// </returns>
        public override IEnumerable<T> Select()
        {
            return
                    CurrentResults().SelectMany(
                            child =>
                            Query(child).FindChildrenByName(Locator.Name).Where(child2 => Query(child2).IsElement()));
        }
    }
}